package beans;

import java.net.Socket;
import java.net.SocketException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class UtilTest
{

	public static void main(String[] args)
	{
		// String[] f = new String[15];
		// f[0] = "a";
		// f[1] = "b";
		// fibonacci(f);

		// getMax(0, 2);

		// Scanner scan = new Scanner(System.in);
		// int n = scan.nextInt();
		// int[][] a = new int[n][n];
		// for (int i = 0; i < a.length; i++)
		// for (int j = 0; j < a[i].length; j++)
		// a[i][j] = scan.nextInt();
		// NxN(n, a);

		// magical(new Scanner(System.in).nextInt());

		// testTimeUtil();
		// testAtomicLong();
		// testBytes();
		// testSocket();

		// testTypeValue();

		// UID("0001", t.stff, "1001");

		System.out.println("“中华人民共和国”的MD5加密后：\n" + getMD5("中华人民共和国"));
		System.out.println("http://www.baidu.com/的MD5加密后：\n" + getMD5("http://www.baidu.com/"));

	}

	public static void UID(String stationid, t t, String id)
	{
		String name = "北京平安力合科技发展股份有限公司";
		String md5 = getMD5(name).substring(0, 16);
		System.out.println(getMD5("平安力合").length() + " : " + getMD5("平安力合"));
		System.out.println(getMD5("pinganlihe").length() + " : " + getMD5("pinganlihe"));
		System.out.println(getMD5(name).length() + " : " + md5);
		String result;
		switch (t)
		{
			case stff:
				result = stationid + md5.substring(0, 4) + id;
				break;
			case oper:
				result = stationid + md5.substring(4, 8) + id;
				break;
			case count:
				result = stationid + md5.substring(8, 12) + id;
				break;
			default:
				result = stationid + md5.substring(12, 16) + id;
				break;
		}
		System.out.println(result);
	}

	enum t
	{
		stff, oper, count
	}

	public static void testTypeValue()
	{
		String a = "12.34";
		float i = Float.parseFloat(a);
		System.out.println(i);
		System.out.println(String.valueOf(12.1111));
		System.out.println(Integer.valueOf("1111"));

		List<Integer> list = new ArrayList<Integer>();
		list.add(0);
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		list = list.subList(1, 3);

		for (Integer integer : list)
		{
			System.out.println(integer);
		}

	}

	public static void testSocket()
	{
		Socket socket = new Socket();
		try
		{
			System.out.println(socket.getReceiveBufferSize());
			System.out.println(socket.getSendBufferSize());
		}
		catch (SocketException e)
		{
			e.printStackTrace();
		}
	}

	/*
	 * 00000000 00000000 00000000 00001010
	 */
	public static void testBytes()
	{
		byte[] bytes = new byte[4];
		int value = 10;
		int offset = 0;

		String t = "00000000";

		bytes[offset] = (byte) (0xFF & (value >> 24));
		bytes[offset + 1] = (byte) (0xFF & (value >> 16));
		bytes[offset + 2] = (byte) (0xFF & (value >> 8));
		bytes[offset + 3] = (byte) (0xFF & value);

		for (byte byte1 : bytes)
		{
			String binary = Integer.toBinaryString(byte1 & 0xFF);
			int l = 8 - binary.length();
			if (binary.length() < 8)
				binary = t.substring(0, l) + binary;
			System.out.print(binary + " ");

		}
	}

	/*
	 * 1000 1000 24 1
	 */
	public static void testTimeUtil()
	{
		System.out.println(TimeUnit.DAYS.toDays(1000));
		System.out.println(TimeUnit.HOURS.toHours(1000));
		System.out.println(TimeUnit.DAYS.toHours(1));
		System.out.println(TimeUnit.HOURS.toDays(24));
	}

	/*
	 * 0 1 2 3
	 */
	public static void testAtomicLong()
	{
		AtomicLong atomiclong = new AtomicLong(0);

		System.out.println(atomiclong.getAndIncrement());
		System.out.println(atomiclong.getAndIncrement());
		System.out.println(atomiclong.getAndIncrement());
		System.out.println(atomiclong.getAndIncrement());
	}

	public static void magical(int num)
	{
		int[][] a = new int[num][num];
		int x = 0, y = 0, m = 0, n = 0;
		for (int i = 1; i <= num * num; i++)
		{

			if (i == 1)
			{
				x = num - 1;
				y = (num - 1) / 2;
			}
			else
			{
				if (++x > (num - 1))
					x = 0;
				if (++y > (num - 1))
					y = 0;
			}
			if (a[x][y] != 0)
			{
				x = m - 1;
				y = n;
			}
			a[x][y] = i;
			m = x;
			n = y;
		}
		for (int i = 0; i < a.length; i++)
		{
			StringBuffer sb = new StringBuffer();
			for (int j = 0; j < a[i].length; j++)
				sb.append(a[i][j] + ",");
			System.out.println(sb.substring(0, sb.length() - 1));
		}
	}

	public static void NxN(int n, int[][] a)
	{
		int temp = 0;
		for (int i = 0; i < a.length; i++)
			for (int j = 0; j < a[i].length; j++)
			{
				if (i == j || i > j)
					continue;
				temp = a[i][j];
				a[i][j] = a[j][i];
				a[j][i] = temp;
			}
		for (int i = 0; i < a.length; i++)
		{
			StringBuffer sb = new StringBuffer();
			sb.append("[");
			for (int j = 0; j < a[i].length; j++)
			{
				sb.append(a[i][j] + ", ");
			}
			sb.replace(sb.length() - 2, sb.length() - 1, "]");
			System.out.println(sb);
		}

	}

	public static void getMax(int a, int b)
	{
		int max = (a + b + Math.abs(a - b)) / 2;
		System.out.println(max);
	}

	public static void fibonacci(String[] f)
	{
		System.out.println(f[0]);
		System.out.println(f[1]);
		for (int i = 2; i < f.length; i++)
		{
			f[i] = f[i - 1] + f[i - 2];
			System.out.println(f[i]);
		}
	}

	public static void shuzuxiabiao()
	{
		int N = 99;
		int[] a = new int[N];
		int[] b = new int[N];
		int sum = 0;
		for (int j = 0; j < N; j++)
		{
			a[j] = new Random().nextInt(99);
			System.out.println("Java.main()" + a[j]);
		}
		for (int j = 0; j < N; j++)
			sum = b[a[j]]++;
		System.out.println("Java.main()" + sum);
	}

	public static String getMD5(String s)
	{
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		try
		{
			byte[] strBytes = s.getBytes();
			// 使用MD5创建MessageDigest对象
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(strBytes);
			byte[] md = md5.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++)
			{
				byte b = md[i];
				// 将每个数(int)b进行双字节加密
				str[k++] = hexDigits[b >> 4 & 0xf];
				str[k++] = hexDigits[b & 0xf];
			}
			return new String(str);
		}
		catch (Exception e)
		{
			return null;
		}
	}

}
